package contacts.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import contacts.controller.ContactsControllerListener;
import contacts.po.Room;
import contacts.po.Student;

public class ContactsFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	private ContactsControllerListener controllerListener;
	private JScrollPane tablePanel; //
	private int selRow=-1;
	public ContactsFrame() {
		this.controllerListener=new ContactsControllerListener();
		this.controllerListener.setContactsFrame(this);
		this.setJMenuBar(getContactsMenuBar());
		this.add(this.getContactsModifyToolBar(),BorderLayout.NORTH);
		this.add(this.getContactsTablePanel(controllerListener.getContactsService().findAllStudent()), BorderLayout.CENTER);
		
		this.setVisible(true);
		//this.setVisible(false);
		this.setSize(1600,600);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.validate();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 this.validate();
	}
	public void refreshStudent(List<Student> studentList ) {
		System.out.println("测试——当前学生人数为："+studentList.size());
		this.remove(tablePanel);
		this.add(this.getContactsTablePanel(controllerListener.getContactsService().findAllStudent()),BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");		
	}
	public void refreshRoom(List<Room> roomList ) {
		System.out.println("测试——当前学生人数为："+roomList.size());
		this.remove(tablePanel);
		this.add(this.getContactsTablePanel2(controllerListener.getContactsService().findAllRoom()),BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
		
	}
	public void showSearchResult(List<Student> result) {
		this.remove(tablePanel);
		this.add(this.getContactsTablePanel(result),BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
		
	}
	
	public void showSearchResult2(List<Room> result) {
		this.remove(tablePanel);
		this.add(this.getContactsTablePanel2(result),BorderLayout.CENTER);
		this.validate();
		JOptionPane.showMessageDialog(this, "查询成功！");
		
	}
	
	private JMenuBar getContactsMenuBar() {
		JMenuItem listRoomMenuItem = new JMenuItem("加载学生宿舍信息");
		JMenuItem listStudentMenuItem = new JMenuItem("加载学生个人信息列表");
		listRoomMenuItem.addActionListener(controllerListener);
		listRoomMenuItem.setActionCommand("refreshRoomFrame");//要改
		listStudentMenuItem.addActionListener(controllerListener);
		listStudentMenuItem.setActionCommand("refreshStudentFrame");//要改
		JMenuBar bar = new JMenuBar();
		JMenu fileMenu = new JMenu("学生信息管理");
		fileMenu.add(listRoomMenuItem);
		fileMenu.add(listStudentMenuItem);
		bar.add(fileMenu);
		return bar;
	}
	
	private JToolBar getContactsModifyToolBar() {
		JToolBar toolBar=new JToolBar();
		toolBar.setFloatable(false);//b - 如果为 true，则工具栏可以移动；如果为 false，则工具栏不能移动 
		toolBar.setBorder(new BevelBorder(BevelBorder.RAISED));
		toolBar.setAlignmentX(RIGHT_ALIGNMENT);
		toolBar.setSize(60, 60);
		
		JButton addStudentButton=new JButton("添加学生");
		ImageIcon addIcon = new ImageIcon(ContactsFrame.class.getResource("/addStudent.jpg"));
		addStudentButton.setIcon(addIcon);
		JButton searchStudentButton=new JButton("查询学生");
		ImageIcon searchIcon = new ImageIcon(ContactsFrame.class.getResource("/searchStudent.jpg"));
		searchStudentButton.setIcon(searchIcon);
		JButton updateStudentButton=new JButton("修改学生");
		ImageIcon updateIcon = new ImageIcon(ContactsFrame.class.getResource("/updateStudent.jpg"));
		updateStudentButton.setIcon(updateIcon);
		JButton deleteStudentButton=new JButton("删除学生");
		ImageIcon deleteIcon = new ImageIcon(ContactsFrame.class.getResource("/deleteStudent.jpg"));
		deleteStudentButton.setIcon(deleteIcon);
		
		addStudentButton.addActionListener(controllerListener);
		addStudentButton.setSize(20,20);
		searchStudentButton.addActionListener(controllerListener);
		searchStudentButton.setSize(20,20);
		updateStudentButton.addActionListener(controllerListener);
		updateStudentButton.setSize(20,20);
		deleteStudentButton.addActionListener(controllerListener);
		deleteStudentButton.setSize(20,20);
		
		addStudentButton.setActionCommand("addStudentDialog");
		searchStudentButton.setActionCommand("searchStudentDialog");
		updateStudentButton.setActionCommand("updateStudentDialog");
		deleteStudentButton.setActionCommand("deleteStudentDialog");
		
		
		
		JButton addRoomButton=new JButton("添加宿舍");
		ImageIcon addIcon2 = new ImageIcon(ContactsFrame.class.getResource("/addRoom.jpg"));
		addRoomButton.setIcon(addIcon2);
		JButton searchRoomButton=new JButton("查询宿舍");
		ImageIcon searchIcon2 = new ImageIcon(ContactsFrame.class.getResource("/searchRoom.jpg"));
		searchRoomButton.setIcon(searchIcon2);
		JButton updateRoomButton=new JButton("修改宿舍");
		ImageIcon updateIcon2 = new ImageIcon(ContactsFrame.class.getResource("/updateRoom.jpg"));
		updateRoomButton.setIcon(updateIcon2);
		JButton deleteRoomButton=new JButton("删除宿舍");
		ImageIcon deleteIcon2 = new ImageIcon(ContactsFrame.class.getResource("/deleteRoom.jpg"));
		deleteRoomButton.setIcon(deleteIcon2);
		
		
		
		addRoomButton.addActionListener(controllerListener);
		addRoomButton.setSize(20,20);
		searchRoomButton.addActionListener(controllerListener);
		searchRoomButton.setSize(20,20);
		updateRoomButton.addActionListener(controllerListener);
		updateRoomButton.setSize(20,20);
		deleteRoomButton.addActionListener(controllerListener);
		deleteRoomButton.setSize(20,20);
		
		addRoomButton.setActionCommand("addRoomDialog");
		searchRoomButton.setActionCommand("searchRoomDialog");
		updateRoomButton.setActionCommand("updateRoomDialog");
		deleteRoomButton.setActionCommand("deleteRoomDialog");
		
		
		
		Dimension size=new Dimension(50,50);
		
		toolBar.add(addStudentButton);
		toolBar.add(new JToolBar.Separator(size));//创建一个新的工具栏分隔符，其默认大小由当前外观定义。 
		toolBar.add(searchStudentButton);
		toolBar.add(new JToolBar.Separator(size));
		toolBar.add(updateStudentButton);
		toolBar.add(new JToolBar.Separator(size));
		toolBar.add(deleteStudentButton);
		toolBar.add(new JToolBar.Separator(size));
		
		toolBar.add(addRoomButton);
		toolBar.add(new JToolBar.Separator(size));//创建一个新的工具栏分隔符，其默认大小由当前外观定义。 
		toolBar.add(searchRoomButton);
		toolBar.add(new JToolBar.Separator(size));
		toolBar.add(updateRoomButton);
		toolBar.add(new JToolBar.Separator(size));
		toolBar.add(deleteRoomButton);
	    return toolBar;
	}
	private JScrollPane getContactsTablePanel(List<Student> studentList) {
		String titles[]= {"序号","学号","姓名","生日","电话号码","邮件","地址","宿舍号","宿舍名"};
		Object data[][]=new Object[studentList.size()][9];
		Iterator<Student> iter=studentList.iterator();
		int i=0;
		while(iter.hasNext()) {
			Student student=iter.next();
			data[i][0]=student.getId();
			data[i][1]=student.getsNumber();
			data[i][2]=student.getsName();
			data[i][3]=student.getBirthday();
			data[i][4]=student.getTel();
			data[i][5]=student.getEmail();
			data[i][6]=student.getHouseAddress();
			data[i][7]=student.getRoom().getRoomNo();			
			data[i][8]=student.getRoom().getRoomName();			
			i++;
		}
		
		DefaultTableModel tableModel=new DefaultTableModel(data,titles);
		JTable table=new JTable(tableModel);
		table.setBackground(Color.pink);
		table.getTableHeader().setFont(new Font("微软雅黑",Font.BOLD,20));
		table.getTableHeader().setBackground(Color.yellow);
		table.setFont(new Font("微软雅黑",Font.PLAIN,20));
		table.getTableHeader().setPreferredSize(new Dimension(20, 40));
	    table.setRowHeight(table.getRowHeight()+20);
	    
	    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    table.repaint();
	    table.addMouseListener(new MouseAdapter() {
	    	public void mouseClicked(final MouseEvent e) {
	    		selRow=table.getSelectedRow();
	    		int id=Integer.parseInt(table.getValueAt(selRow,0).toString().trim());
	    		String sNumber=table.getValueAt(selRow,1).toString().trim();
	    		String sName=table.getValueAt(selRow,2).toString().trim();
	    		Date birthday= Date.valueOf(table.getValueAt(selRow,3).toString().trim());	    		
	    		String tel=table.getValueAt(selRow,4).toString().trim();
	    		String email=table.getValueAt(selRow,5).toString().trim();
	    		String houseAddress=table.getValueAt(selRow,6).toString().trim();
	    		int sRoomNo=Integer.parseInt(table.getValueAt(selRow,7).toString().trim());	    		
	    		String sRoomName=table.getValueAt(selRow,8).toString().trim();	    		
	    		System.out.println(id+sNumber+sName+birthday+tel+email+houseAddress+sRoomNo);
	    	}
	    });
	    tablePanel=new JScrollPane(table);
	    return tablePanel;
	    
	}
	
	private JScrollPane getContactsTablePanel2(List<Room> roomList) {
		String titles[]= {"宿舍号","宿舍名"};
		Object data[][]=new Object[roomList.size()][2];
		Iterator<Room> iter=roomList.iterator();
		int m=0;
		while(iter.hasNext()) {
			Room room=iter.next();
			data[m][0]=room.getRoomNo();
			data[m][1]=room.getRoomName();	
			m++;
		}
		DefaultTableModel tableModel2=new DefaultTableModel(data,titles);
		JTable table2=new JTable(tableModel2);
		table2.setBackground(Color.pink);
		table2.getTableHeader().setFont(new Font("微软雅黑",Font.BOLD,20));
		table2.getTableHeader().setBackground(Color.yellow);
		table2.setFont(new Font("微软雅黑",Font.PLAIN,20));
		table2.getTableHeader().setPreferredSize(new Dimension(20, 40));
	    table2.setRowHeight(table2.getRowHeight()+20);
	    
	    table2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    table2.repaint();
	    table2.addMouseListener(new MouseAdapter() {
	    	public void mouseClicked(final MouseEvent e) {
	    		selRow=table2.getSelectedRow();
	    		int sRoomNo=Integer.parseInt(table2.getValueAt(selRow,0).toString().trim());
	    		String sRoomName=table2.getValueAt(selRow,1).toString().trim();    		   		
	    		System.out.println(sRoomNo+sRoomName);
	    	}
	    });
		tablePanel=new JScrollPane(table2);
	    return tablePanel;
	}
	public ContactsControllerListener getControllerListener() {
		return controllerListener;	
	}
	public int getSelRow() {
		return selRow;
	}
}
